package s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.index.IndexWriter;

import com.mysql.jdbc.Statement;


import cbir.MyFileFilter;


public class AdminTask extends HttpServlet{

		private static final long serialVersionUID = 1L;

		private Properties props;
		private String propsPath;

		@Override
		public void init(ServletConfig config) throws ServletException {
			super.init(config);
			
			this.propsPath = this.getServletContext().getRealPath("/");
			this.props = new Properties();
			try {
				props.loadFromXML(new FileInputStream(propsPath + "properties.properties"));
			} catch (FileNotFoundException e) {e.printStackTrace();} 
			catch (IOException e) {e.printStackTrace();}
	}
		
		
		@SuppressWarnings("deprecation")
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
			
				String task = (String)req.getParameter("task");
				
				if(task.compareTo("clean") == 0){
						File dir = new File(this.getServletContext().getRealPath("/") +  props.getProperty("IMG_PATH"));
						File[] fileList = dir.listFiles(new MyFileFilter());
						for(int i = 0; i < fileList.length; i++){
							if(fileList[i].isDirectory() == false){
								if(fileList[i].delete() == false)
									System.err.println("Fail while removing the file");
							}
						}
						
						dir = new File(this.getServletContext().getRealPath("/") +   props.getProperty("IMG_PATH") + "thumb/");
						fileList = dir.listFiles(new MyFileFilter());
						for(int i = 0; i < fileList.length; i++){
							if(fileList[i].delete() == false)
								System.err.println("Fail while removing the file");
						}
						
						dir = new File(this.getServletContext().getRealPath("/") +   props.getProperty("INDEX_PATH"));
						fileList = dir.listFiles(new MyFileFilter());
						for(int i = 0; i < fileList.length; i++){
							if(fileList[i].delete() == false)
								System.err.println("Fail while removing the file");
						}
						
						dir = new File(this.getServletContext().getRealPath("/") +   props.getProperty("IMG_PATH_NOMEM"));
						fileList = dir.listFiles(new MyFileFilter());
						for(int i = 0; i < fileList.length; i++){
							if(fileList[i].isDirectory() == false){
								if(fileList[i].delete() == false)
									System.err.println("Fail while removing the file");
							}
						}
						
						dir = new File(this.getServletContext().getRealPath("/") +  props.getProperty("IMG_PATH_NOMEM") + "thumb/");
						fileList = dir.listFiles(new MyFileFilter());
						for(int i = 0; i < fileList.length; i++){
							if(fileList[i].delete() == false)
								System.err.println("Fail while removing the file");
						}
						
						dir = new File(this.getServletContext().getRealPath("/") +  props.getProperty("INDEX_PATH_NOMEM"));
						fileList = dir.listFiles(new MyFileFilter());
						for(int i = 0; i < fileList.length; i++){
							if(fileList[i].delete() == false)
								System.err.println("Fail while removing the file");
						}
						
						try {
							Class.forName("com.mysql.jdbc.Driver");
							Connection connection = DriverManager.getConnection(props.getProperty("DB_URL"), props.getProperty("DB_USER"), props.getProperty("DB_PASS"));
							Statement statement = (Statement) connection.createStatement();
							statement.executeQuery("TRUNCATE TABLE imgTag;"); 
							statement.executeQuery("TRUNCATE TABLE img;"); 
							statement.executeQuery("TRUNCATE TABLE tag;"); 
						}
						catch (SQLException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}
						
						props.setProperty("IS_CLEAN", "true");
						props.storeToXML(new FileOutputStream(new File(propsPath + "properties.properties")), "CBIR_properties");
						PrintWriter pw = res.getWriter();
						pw.print("OK");
					
				}
				else if (task.compareTo("optimize") == 0){
					
						IndexWriter iw = new IndexWriter(this.getServletContext().getRealPath("/") + props.getProperty("INDEX_PATH"), new SimpleAnalyzer(), false);
						iw.optimize();
						iw.flush();
						iw.close();						
						PrintWriter pw = res.getWriter();
						pw.print("OK");
					
				}
				else if (task.compareTo("cleanTags") == 0){
						
						Connection connection = null;
						try {
							Class.forName("com.mysql.jdbc.Driver");
							connection = DriverManager.getConnection(props.getProperty("DB_URL"), props.getProperty("DB_USER"), props.getProperty("DB_PASS"));
							Statement statement = (Statement) connection.createStatement();
							statement.executeQuery("TRUNCATE TABLE imgTag;"); 
							statement.executeQuery("TRUNCATE TABLE tag;"); 
						}
						catch (SQLException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}

						PrintWriter pw = res.getWriter();
						pw.print("OK");
						
				}
		}
}